<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
/**
 * 运费逻辑
 */
class Shipping extends Controller
{
    public $user_id = 0;
    public $user = array();
    public $check_date = 0;

    public function _initialize() {      
        parent::_initialize();
        if(session('?user'))
        {
            $user = session('user');
            $user = Db::table('users')->where("id", $user['id'])->find();
            session('user',$user);            
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user',$user); //存储用户信息
            $this->assign('user_id',$this->user_id);
        }
    }
    
    /**
     * 2018-4-11 日期格式
     * 曹振威 开发人
     * 添加运费模板
     */
    public function add_shipping() {
        // 用户id   商户id   系统id
//        $user_id = $this->user_id;
        $user_id = 1;
        $system_id = input('system_id');
        $business_id = Db::table('business')->where('business_id',$user_id)->field('id')->find();
        
        $template_name = input('post.template_name');  // 模板名称
        $delivery_time = input('post.delivery_time');  // 发货时间
        $delivery_area = input('post.delivery_area'); // 发货区域
        $pattern = input('post.pattern'); // 是否包邮；0 用户自付  1 免费包邮
        $type = input('post.type'); // 0 按件数 1 按数量 2 按体积
        $fare = input('post.fare'); // 默认运费
        $renewals_num = input('post.renewals_num'); // 续件重量
        $renewals_fare = input('post.renewals_fare'); // 续件运费
        $delivery_mode = input('post.delivery_mode',0);
        if(!$system_id){
            $this->check_date = 1;
            $date = array('code'=>20019,'msg'=>'没有此系统','data'=>'');
        }elseif(!$business_id){
            $this->check_date = 1;
            $date = array('code'=>20020,'msg'=>'该用户是非商户','data'=>'');
        }elseif (!$template_name || !$delivery_time || $pattern === '' || !$type || !$fare || !$renewals_num || !$renewals_fare) {
            $this->check_date = 1;
            $date = array('code'=>20021,'msg'=>'参数有误','data'=>'');
        }
        if($this->check_date != 0){
            exit(json_encode($date));
        }
        $express_insert = [
            'system_id' => $system_id,
            'business_id' => $business_id['id'],
            'template_name' => $template_name,
            'delivery_area' => $delivery_area,
            'delivery_time' => $delivery_time,
            'pattern' => $pattern,
            'type' => $type,
            'fare' => $fare,
            'renewals_num' => $renewals_num,
            'renewals_fare' => $renewals_fare,
            'createtime' => date('Y-m-d H:i:s')
        ];
        if($express_insert){
            $where['system_id'] = $system_id;
            $where['business_id'] = $business_id['id'];
            $where['state'] = 0;
            $row = Db::table('express')->insert($express_insert);
            $data = Db::table('express')->where($where)->field('id,system_id,business_id,template_name,delivery_area,fare,renewals_fare,renewals_num,delivery_mode')->select();
            if($row){
                $date = array('code'=>666,'msg'=>'快递模板插入成功','data'=>$data);
            }else{
                $date = array('code'=>20029,'msg'=>'快递模板插入是败','data'=>'');
            }
        }else{
            $date = array('code'=>20030,'msg'=>'未获取到要插入的数据','data'=>'');
        }
        return $date;
    }
    
    /**
     * 2018-4-11 日期格式
     * 曹振威 开发人
     * 删除运费模板
     */
    public function del_shipping() {
        $user_id = $this->user_id;
        $user_id = 1;
        $system_id = input('system_id','');
        $business_id = Db::table('business')->where('business_id',$user_id)->field('id')->find();
        $id = input('id');
        if(!$system_id){
            $this->check_date = 1;
            $date = array('code'=>20019,'msg'=>'没有此系统','data'=>'');
        }elseif(!$business_id){
            $this->check_date = 1;
            $date = array('code'=>20020,'msg'=>'该用户是非商户','data'=>'');
        }elseif (!$id) {
            $this->check_date = 1;
            $date = array('code'=>20021,'msg'=>'非法操作','data'=>'');
        }
        if($this->check_date != 0){
            exit(json_encode($date));
        }
        $where = [
            'system_id' => $system_id,
            'business_id' => $business_id['id'],
            'id' => $id
        ];
        $row = Db::table('express')->where($where)->setField('state',1);
        if($row){
            $date = array('code'=>666,'msg'=>'模板删除成功','data'=>'');
        }else{
            $date = array('code'=>20031,'msg'=>'模板删除失败','data'=>'');
        }
        return $date;
    }
}
